<template>
  <div>
    <h1>三级分类页面</h1>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"> </el-input>
    <el-tree
      :data="data"
      :props="defaultProps"
      :expand-on-click-node="false"
      :filter-node-method="filterNode"
      @node-click="nodeClick"
      ref="tree"
    >
    </el-tree>
  </div>
</template>
<script>
export default {
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  data() {
    return {
      filterText: "",
      category: {
        sort: 0,
        showStatus: 1,
      },
      dialogFormVisible: false,
      data: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    nodeClick(node){
      if (node.catLevel == 3) {
        // 需要给父组件传递数据
        this.$emit("tree-node-click",node);
      }
    },
    //过滤结点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    //声明一个方法去查询商品服务得到三级分类结果
    //axios
    getDataList() {
      //请求商品服务查询三级列表
      this.$http({
        url: this.$http.adornUrl("/product/category/tree/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.data = data.data;
        console.log(this.data);
      });
    },
  },
  created() {
    this.getDataList();
  }
};
</script>
